$(function () {
    layui.use(['element', 'table', 'layer', 'laytpl', 'form','util'], function () {
        var element = layui.element,
            table = layui.table,
            layer = layui.layer,
            laytpl = layui.laytpl,
            form = layui.form,
            util = layui.util;
        var checkData = [], gatewayList = [];
        getAllGateway();
        getSensorData();

        //查询全部用户
        function getSensorData() {
            $.ajax({
                type: "post",
                url: ctx + "/sensorManage/getAll.do",
                data: {},
                dataType: "json",
                success: function (data) {
                    renderTable(data);
                }
            });
        }

        function renderTable(data) {
            var tbHeigth = $('.settings-wrap').height();
            table.render({
                elem: '#sensorTable',
                data: data,
                height: tbHeigth,
                title: '传感器信息表',
                toolbar: '#toolbar',
                defaultToolbar: [],
                cols: [[
                    {type: 'radio', fixed: 'left'}
                    , {field: 'readerCode', title: '仪表识别码'}
                    , {field: 'gatewayName', title: 'IP简称'}
                    , {field: 'descrition', title: '备注'}
                    , {field: 'addedTime', title: '上传时间', templet:'<div>{{ layui.util.toDateString(d.addedTime) }}</div>'}
                ]],
                page: {
                    layout: ['prev', 'page', 'next', 'count', 'skip', 'limit']
                    , groups: 5 //只显示 5 个连续页码
                    , limit: 15  //每页显示条数
                    , limits: [15, 30, 45, 60]
                },
                loading: true,
                done: function () {
                    $('.layui-table-view > .layui-table-box').height(tbHeigth - 41);
                    $('.layui-table-view > .layui-table-box .layui-table-body').height(tbHeigth - 80);
                }
            });
        }

        //头工具栏事件
        table.on('toolbar(sensorTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch (obj.event) {
                case 'getCheckData':
                    checkData = checkStatus.data;  //获取选中行数据
                    // console.log(checkData);
                    break;
            };
        });

        // 新增
        $('.addBtn').click(function () {
            var layerIdx = layer.open({
                type: 1,
                title: "新增",
                closeBtn: 1,
                anim: 0,
                area: 'auto',
                content: $('#add-tpl').html(),
                success: function (layero) {
                    var getTpl = $('#gateway-tpl').html();
                    laytpl(getTpl).render(gatewayList, function (html) {
                        layero.find('.gatewayMenu').html(html);
                        form.render();
                    });
                }
            });
            form.on('submit(add-param)', function (res) {
                // console.log(res.field); //当前容器的全部表单字段，名值对形式：{name: value}
                addSensor(res.field);
                layer.close(layerIdx);
                return false;
            });
            $(".add-param button[type='cancel']").click(function () {
                layer.close(layerIdx);
                return false;
            });
        });

        // 编辑
        $('.editBtn').click(function () {
            $('.getCheckData').click();
            if (checkData.length === 1) {
                var layerIdx = layer.open({
                    type: 1,
                    title: "编辑",
                    closeBtn: 1,
                    anim: 0,
                    area: 'auto',
                    content: $('#edit-tpl').html(),
                    success: function (layero) {
                        var getTpl = $('#gateway-tpl').html();
                        laytpl(getTpl).render(gatewayList, function (html) {
                            layero.find('.gatewayMenu').html(html);
                            form.render();
                        });
                    }
                });
                form.val("edit-param", checkData[0]);
                form.on('submit(edit-param)', function (res) {
                    // console.log(res.field); //当前容器的全部表单字段，名值对形式：{name: value}
                    updateSensor(res.field);
                    layer.close(layerIdx);
                    return false;
                });
                $(".edit-param button[type='cancel']").click(function () {
                    layer.close(layerIdx);
                    return false;
                });
            } else {
                layer.alert('请选择一条数据')
            }
        });

        // 删除
        $('.delBtn').click(function () {
            $('.getCheckData').click();
            if (checkData.length === 1) {
                var layerIdx = layer.confirm('确认删除?', function (index) {
                    // console.log(checkData[0]);
                    deleteSensor(checkData[0]);
                    layer.close(index);
                });
            } else {
                layer.alert('请选择一条数据')
            }
        });


        // 增
        function addSensor(data) {
            $.ajax({
                type: "GET",
                url: ctx + "/sensorManage/add.do",
                data: data,
                dataType: "json",
                success: function (result) {
                    if (result.code == '000000') {
                        getSensorData();
                    } else {
                        layer.alert(result.message)
                    }
                }
            });
        }

        //删
        function deleteSensor(data) {
            $.ajax({
                type: "GET",
                url: ctx + "/sensorManage/delete.do",
                data: {"readerCode": data.readerCode},
                dataType: "json",
                success: function (result) {
                    if (result.code == '000000') {
                        getSensorData();
                    } else {
                        layer.alert(result.message);
                    }
                }
            });
        }

        // 改
        function updateSensor(obj) {
            $.ajax({
                url: ctx + "/sensorManage/update.do",
                type: "post",
                data: obj,
                success: function (result) {
                    if (result.code == '000000') {
                        getSensorData();
                    } else {
                        layer.alert(result.message);
                    }
                }
            });
        }

        // 获取网关list
        function getAllGateway() {
            $.ajax({
                type: "GET",
                url: ctx + "/gatewayManage/getAll.do",
                dataType: "json",
                success: function (result) {
                    if (result != null) {
                        gatewayList = result;
                    }
                }
            });
        }

    });
});


